//
//  ActionState.m
//  INFSWA02
//
//  Created by Chris Kievit on 10-10-11.
//  Copyright 2011 Pro4all. All rights reserved.
//

#import "MainViewController.h"
#import "ActionState.h"
#import "Board.h"
#import "Square.h"
#import "Piece.h"
#import "MoveCommand.h"
#import "AttackCommand.h"

@implementation ActionState

@synthesize selectedPiece;

- (id)init
{
    self = [super init];
    if (self) 
    {
        selectedPiece = nil;
    }
    
    return self;
}

- (void)didSelectSquare:(Square *)square onBoard:(Board *)board
{
    if([square hasPiece]) 
    {
        Piece *piece = [square piece];
        
        //If selected piece is an enemy, attack.
        if (![piece belongsToPlayer:board.currentPlayer])
        {
            AttackCommand *attackCommand = [[AttackCommand alloc] initWithSquareSource:[board squareForPiece:selectedPiece] destination:square];
            [attackCommand setBoard:board];
            
            if([attackCommand execute])
                [board saveState:attackCommand];
        } 
        else if (piece != selectedPiece)
        {
            NSLog(@"Buff buff");
        } 
        else
        {
            NSLog(@"%@", [selectedPiece description]);
            NSLog(@"deselect");
        }
    } 
    else 
    {
        MoveCommand *moveCommand = [[MoveCommand alloc] initWithSquareSource:[board squareForPiece:selectedPiece] destination:square];
        [moveCommand setBoard:board];
        
        if([moveCommand execute])
            [board saveState:moveCommand];
    }
    
    [board updateButtonTitles];
    [board setCurrentState:[board noState]];
}

@end
